import { defineStore } from 'pinia'
import { $utils } from '@utils'
import { useGameStore } from './game'
import { getLotteryTemplate } from '@/api'

export const usePcnnStore = defineStore('pcnn', {
  state: () => ({
    gameTemplateData: [], //游戏渲染数据
    gameComponentTag: 'XglhcTmzPlay', // 游戏组件标签
    currentGameType: 0, //当前游戏索引
    gameStore: useGameStore(),
  }),

  getters: {
    // 当前选中游戏组件
    getGameComponentTag() {
      return this.getPlayTypeCodeList[this.currentGameType]
    },

    // PCNN所有游戏玩法
    getPlayTypeCodeList() {
      // [ "XAB_GAME"] 值是这样子
      if ($utils.isEmpty(this.gameTemplateData)) return []
      const list = this.gameTemplateData.map((game) => game.playTypeCode + '_' + 'GAME')
      return list
    },
    // 获得 选中游戏类型数据
    getCurrentGameData() {
      if ($utils.isEmpty(this.gameTemplateData)) return []
      return this.gameTemplateData[this.currentGameType]
    },

    // // 选中子游戏的类型
    getSonGameType() {
      if ($utils.isEmpty(this.getCurrentGameData)) return ''
      return ['XAB', 'SH']
    },
  },

  actions: {
    async init() {
      if ($utils.isEmpty(this.gameTemplateData)) {
        await this.fetchGameTemplateData()
      }

      this.gameStore.setCurrentSonGame(this.getSonGameType)
    },

    // 当前游戏索引
    setCurrentGameType(index) {
      this.currentGameType = index

      this.gameStore.setCurrentSonGame(this.getSonGameType)
    },

    // 立即下注
    betNow(refs) {
      refs['gameRef'].betNow()
    },

    // 清空所有下注内容
    clearInvestList(refs) {
      refs['gameRef'].updateInvestList('clear')
    },

    // 删除一个游戏的下注订单
    deleteAnInvest(data) {
      data.dom['gameRef'].updateInvestList('del', data.data)
    },

    // 撤消订单
    withdrawAnOrder() {},

    // 获得游戏渲染数据
    async fetchGameTemplateData() {
      const res = await getLotteryTemplate({ lotteryCode: this.gameStore.currentGame })
      this.gameTemplateData = res.data
      this.gameStore.setCurrentSonGame(this.getSonGameType)
    },
  },
})
